package health

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/health"
	healthReq "gitee.com/g-qs/gin-vue-admin/server/model/health/request"
)

type ShipuService struct {
}

// CreateShipu 创建Shipu记录
// Author [piexlmax](https://github.com/piexlmax)
func (shipuService *ShipuService) CreateShipu(shipu *health.Shipu) (err error) {
	err = global.GVA_DB.Create(shipu).Error
	return err
}

// DeleteShipu 删除Shipu记录
// Author [piexlmax](https://github.com/piexlmax)
func (shipuService *ShipuService) DeleteShipu(shipu health.Shipu) (err error) {
	err = global.GVA_DB.Delete(&shipu).Error
	return err
}

// DeleteShipuByIds 批量删除Shipu记录
// Author [piexlmax](https://github.com/piexlmax)
func (shipuService *ShipuService) DeleteShipuByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]health.Shipu{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateShipu 更新Shipu记录
// Author [piexlmax](https://github.com/piexlmax)
func (shipuService *ShipuService) UpdateShipu(shipu health.Shipu) (err error) {
	err = global.GVA_DB.Save(&shipu).Error
	return err
}

// GetShipu 根据id获取Shipu记录
// Author [piexlmax](https://github.com/piexlmax)
func (shipuService *ShipuService) GetShipu(id uint) (shipu health.Shipu, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&shipu).Error
	return
}

// GetShipuInfoList 分页获取Shipu记录
// Author [piexlmax](https://github.com/piexlmax)
func (shipuService *ShipuService) GetShipuInfoList(info healthReq.ShipuSearch) (list []health.Shipu, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&health.Shipu{})
	var shipus []health.Shipu
	// 如果有条件搜索 下方会自动创建搜索语句

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&shipus).Error
	return shipus, total, err
}
